package xupt.com.bolg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import xupt.com.bolg.entity.Bolg;
import xupt.com.bolg.entity.PageInfor;
import xupt.com.bolg.entity.User;
import xupt.com.bolg.repository.UserRepository;
import xupt.com.bolg.service.impl.BolgServiceImpl;
import xupt.com.bolg.service.impl.UserServiceImpl;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserRepository userRepository;
    @Autowired
    BolgServiceImpl bolgService;
    @Autowired
    UserServiceImpl userService;
    @GetMapping("/updateuser")
    public String skipUpUser(Model model,HttpServletRequest request){
        String name =(String ) request.getSession().getAttribute("name");
        User byName = userService.findByName(name);
        model.addAttribute("user",byName);
        return "user/userinfor";
    }

    @PostMapping("/update/user")
    public String updateUser(User user,HttpServletRequest request){
        String  name =(String ) request.getSession().getAttribute("name");
        User byName = userService.findByName(name);
        if(user.getPassword()!=null){
            byName.setPassword(user.getPassword());
        }
        if(user.getPhone()!=null){
            byName.setPhone(user.getPhone());
        }
        if(user.getEmail()!=null){
            byName.setEmail(user.getEmail());
        }
        boolean update = userService.update(byName);
        if(update){
            return "homepage";
        }else {
            request.setAttribute("msg","信息填写错误 ");
            return "user/userinfor";
        }
    }
    @GetMapping("/user/home")
    public String skipUserHome(Model model,HttpServletRequest request){
        String name =(String) request.getSession().getAttribute("name");
        List<Bolg> all = bolgService.findAll();
        List<Bolg> bolgs=new ArrayList<>();
        for(int i=0;i<all.size();i++){
            if(all.get(i).getUserName().equals(name)){
                bolgs.add(all.get(i));
            }
        }
        model.addAttribute("bolgs",bolgs);
        return "user/userhome";
    }

    //分页,默认分页为一页大小为10页
    @GetMapping("/manager/look/user")
    public String  findByName(PageInfor pageInfor, Model model, HttpServletRequest request){
        String name=null;
        Integer pageNumber=0,size=10;
        if(pageInfor.getSize()!=null&&pageInfor.getSize()>=0){
            size=pageInfor.getSize();
        }
        if(pageInfor.getPageNumber()!=null&&pageInfor.getPageNumber()!=0){
            pageNumber=pageInfor.getPageNumber();
        }
        if(pageInfor.getWord()!=null){
           name=pageInfor.getWord();
        }


        User user=new User();
        user.setName(name);


        //设置匹配器; 匹配的规则
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("id","password","look","manager","phone","email","data");
        Example<User> ex=Example.of(user,matcher);  //设置example
        //设置page页面大小等信息
        Pageable pageable= PageRequest.of(pageNumber,size, Sort.Direction.ASC,"id");
        Page<User> all = userRepository.findAll(ex, pageable);
        int number = all.getNumber()+1;//设置下一页是
        //发送下一页，和关键字信息，页面大小，总页数
        request.setAttribute("pageNumber",number);
        request.setAttribute("word",name);
        request.setAttribute("size",size);
        Integer totalPages = all.getTotalPages();
        request.setAttribute("totalPages",totalPages);



        //返回当前页面的信息
        model.addAttribute("users",all.getContent());
        System.out.println("----user.setName"+user.getName());
        for(int i=0;i<all.getContent().size();i++){
            System.out.println(all.getContent().get(i));
        }

        return "user/list";
    }

}
